/*
  泡泡类，用于表示游戏中的每个泡泡
*/
export class Tile {
  name: string = "tile";//名称
  x: number; // 泡泡坐标x
  y: number; // 泡泡坐标y
  l: number = 0; // 泡泡屏幕left
  t: number = 0; // 泡泡屏幕top
  color: string;
  dom: HTMLDivElement | null = null; // 泡泡dom对象
  paoRadius: number = 15; // 泡泡半径
  paoSize: number = this.paoRadius * 2; // 泡泡直径
  yd: number = Math.sqrt(3) * this.paoRadius; // (Math.sqrt(3) / 2) * (2 * this.paoRadius)六边形排列中的,两个相邻泡泡的垂直间距

  constructor(position: { x: number; y: number; }, value: string) {
    this.x = position.x;
    this.y = position.y;
    this.color = value;
    this.computeStyleOffset();
  }

  computeStyleOffset() {
    this.t = this.yd * this.x;
    if (this.x % 2 == 0) {
      this.l = this.paoSize * this.y;
    } else {
      // 如果是奇数行，则整体向右偏移半个泡泡宽度
      this.l = this.paoSize * this.y + this.paoRadius;
    }
  }

  updatePosition(position: { x: number; y: number; }) {
    this.x = position.x;
    this.y = position.y;
    this.computeStyleOffset();
  }

  updateStylePosition(p: { l: number; t: number; }) {
    this.l = p.l;
    this.t = p.t;
  }
  updateDom(dom: HTMLDivElement) {
    this.dom = dom;
  }

  serialize() {
    // 序列化返回位置和值对象
    return {
      position: {
        x: this.x,
        y: this.y
      },
      value: this.color,
    };
  }
}